package cn.itcast;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;

public class Demo {

	/**
	 * @param args
	 * @throws Exception 
	 */
	public static void main(String[] args) throws Exception {
		write();
		
	}
	
	//提交http请求给服务器  请求本服务的ServletDemo1的类 
	public static void write() throws Exception{
		
		java.net.URL url = new java.net.URL("http://localhost:9090/day08/servlet/ServletDemo1");
		HttpURLConnection conn = (HttpURLConnection) url.openConnection();
		conn.setDoOutput(true);
		//给方法传递key和value的值
		conn.setRequestProperty("xxx", "yyy");
		OutputStream out = conn.getOutputStream();
		out.write("name=flx".getBytes());
		
		conn.getResponseCode();
		
	}
	
	//获取http响应
	public static void read() throws Exception{
		java.net.URL url = new java.net.URL("http://localhost:9090/day08/servlet/ServletDemo1");
		HttpURLConnection conn = (HttpURLConnection) url.openConnection();
		
		System.out.println(conn.getResponseCode());
		System.out.println(conn.getHeaderField("Server"));
		
		BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
		String content = br.readLine();
		
		
		
		System.out.println(content);
	}

}

